package util;

/**
 * @author Neil Dickson
 */
public final class MemoryUtil {
	/**
	 * Calls {@link System#gc()} iff the heap is 50% full or more
	 * @return true iff System.gc() was called
	 */
	public static boolean gc() {
		Runtime runtime = Runtime.getRuntime();
		long free = runtime.freeMemory();
		long total = runtime.totalMemory();
		if (free+free <= total) {
			System.gc();
			return true;
		}
		return false;
	}

	public static void logMemory() {
		Runtime runtime = Runtime.getRuntime();
		long free = runtime.freeMemory();
		long total = runtime.totalMemory();
		System.out.println("Memory Use: "+((total-free+0x80000)>>>20)+"/"+((total+0x80000)>>>20)+"MB");
	}
}
